package com.bitcola.security.activity.biz;

import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.bitcola.entity.ApplyProject;
import com.bitcola.entity.LockPisition;
import com.bitcola.entity.UnlockPositionLog;
import com.bitcola.security.activity.mapper.ColaUserBalanceMapper;
import com.bitcola.security.activity.mapper.LabsApplyMapper;
import com.bitcola.security.activity.mapper.LockPositionLogMapper;
import com.bitcola.security.activity.mapper.LockPositionMapper;
import com.bitcola.security.auth.common.util.EncoderUtil;
import com.bitcola.security.common.util.Snowflake;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service

public class LockBiz {
    @Autowired
    LockPositionMapper lockPositionMapper;
    Snowflake snowflake=new Snowflake(4,11);
    @Autowired
    ColaUserBalanceMapper colaUserBalanceMapper;
    @Autowired
LockPositionLogMapper lockPositionLogMapper;
        @Autowired
        LabsApplyMapper labsApplyMapper;
        @Cached(cacheType = CacheType.LOCAL,expire = 100)
        public ApplyProject getById(String id){
            return labsApplyMapper.selectByPrimaryKey(id);

        }


        @Transactional
        public LockPisition createLockPosition(String projectId,String userId, BigDecimal number,long interval,BigDecimal percent,String coinCode,String icon){

            LockPisition lockPisition=new LockPisition();
            lockPisition.setId(snowflake.nextIdStr());
            lockPisition.setCoinCode(coinCode);
            lockPisition.setCreateTime(new Date());
        lockPisition.setInterval(interval);
        lockPisition.setIcon(icon);
        lockPisition.setNextReleaseTime(new Date(System.currentTimeMillis()+interval));
        lockPisition.setNumber(number);
        lockPisition.setUserId(userId);
        lockPisition.setPercent(percent);
        lockPisition.setRemind(number);
        lockPisition.setProjectId(projectId);
        lockPisition.setStatus(0);
        lockPositionMapper.insert(lockPisition);
        return lockPisition;

    }


    @Scheduled(cron = "0 0/1 * * * ?")
    public synchronized void unLock()
    {
        var now=new Date();
        Example example=new Example(LockPisition.class);
        example.createCriteria().andLessThan("nextReleaseTime",now);
        List<LockPisition>unlock=lockPositionMapper.selectByExample(example);
        for(LockPisition lockPisition:unlock){

            unLockOne(now, lockPisition);
        }


    }
    @Transactional
    public void unLockOne(Date now, LockPisition lockPisition) {
        var userId=lockPisition.getUserId();
        var coinCode=lockPisition.getCoinCode();
        var remind=lockPisition.getRemind();
        var change=lockPisition.getNumber().multiply(lockPisition.getPercent());
        if(remind.subtract(change).compareTo(BigDecimal.ZERO)>=0){
            lockPisition.setRemind(remind.subtract(change));
            lockPisition.setNextReleaseTime(new Date(now.getTime()+lockPisition.getInterval()));
            colaUserBalanceMapper.changeUserBanlance(userId,coinCode,change,BigDecimal.ZERO,BigDecimal.ZERO,BigDecimal.ZERO,BigDecimal.ZERO,BigDecimal.ZERO, EncoderUtil.BALANCE_KEY);

            UnlockPositionLog unlockPositionLog=new UnlockPositionLog();
            unlockPositionLog.setId(snowflake.nextIdStr());
            unlockPositionLog.setAmount(change);
            unlockPositionLog.setCoinCode(coinCode);
            unlockPositionLog.setCreateTime(now);
            unlockPositionLog.setLockId(lockPisition.getId());
            unlockPositionLog.setProjectId(lockPisition.getProjectId());
            unlockPositionLog.setUserId(userId);
            var project=getById(lockPisition.getProjectId());
            if(project!=null) unlockPositionLog.setIcon(getById(lockPisition.getProjectId()).getCoinCodeIcon());
            lockPositionLogMapper.insert(unlockPositionLog);


        }
        lockPisition.setStatus(0);
        if(remind.compareTo(BigDecimal.ZERO)<=0){
            lockPisition.setNextReleaseTime(null);
        }
        lockPositionMapper.updateByPrimaryKey(lockPisition);
    }

    public List<UnlockPositionLog>queryLog(String userId,Date endTime,int size)
    {

        if(endTime==null)endTime=new Date();
        Example example = new Example(UnlockPositionLog.class);
        example.createCriteria().andLessThan("createTime", endTime).andEqualTo("userId",userId);
        example.orderBy("createTime").desc();
        RowBounds rowBounds=new RowBounds(0,size);

        return lockPositionLogMapper.selectByExampleAndRowBounds(example,rowBounds);
    }

    public List<LockPisition>getAllLock(String userId){
        Example example = new Example(LockPisition.class);
        example.createCriteria().andEqualTo("userId", userId);

        return lockPositionMapper.selectByExample(example);

    }







}
